package io.renren.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import io.renren.entity.DictJobEntity;
import io.renren.entity.SysUserEntity;
import io.renren.entity.xtd.DictJobEntityXtd;
import io.renren.service.DictJobService;
import io.renren.service.SysWorkLogService;
import io.renren.service.xtd.DictEmployeeServiceXtd;
import io.renren.service.xtd.DictJobServiceXtd;
import io.renren.utils.Common;
import io.renren.utils.PageUtils;
import io.renren.utils.R;
import io.renren.utils.ShiroUtils;


/**
 * 岗位表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-05-20 15:24:06
 */
@Controller
@RequestMapping("dictjob")
public class DictJobController {
	@Autowired
	private DictEmployeeServiceXtd dictEmployeeServiceXtd;
    @Autowired
	private SysWorkLogService sysWorkLogService;
	@Autowired
	private DictJobService dictJobService;
	@Autowired
	private DictJobServiceXtd dictJobServiceXtd;
	
	@RequestMapping("/dictjob.html")
	public String list(){
		return "dictjob/dictjob.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("dictjob:list")
	public R list(Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<DictJobEntity> dictJobList = dictJobService.queryList(map);
		int total = dictJobService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(dictJobList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("dictjob:info")
	public R info(@PathVariable("id") String id){
		DictJobEntity dictJob = dictJobService.queryObject(id);
		
		return R.ok().put("dictJob", dictJob);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("dictjob:save")
	public R save(@RequestBody DictJobEntity dictJob){
		dictJobService.save(dictJob);
		
		return R.ok();
	}
	
	
	@ResponseBody
	@RequestMapping("/saveXtd")
	@RequiresPermissions("dictjob:save")
	public R saveXtd(@RequestBody DictJobEntityXtd dictJob){
		SysUserEntity user =ShiroUtils.getUserEntity();
		if(user==null || StringUtils.isBlank(user.getUsername())){
			return R.error("账号已失效，请重新登录");
		}
		Date nowData=new Date();
		dictJob.setCreateby(user.getUsername());
		dictJob.setCreatetime(nowData);
		dictJobServiceXtd.saveXtd(dictJob);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("dictjob:update")
	public R update(@RequestBody DictJobEntity dictJob){
		SysUserEntity user =ShiroUtils.getUserEntity();
		if(user==null || StringUtils.isBlank(user.getUsername())){
			Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
        			ShiroUtils.getUserEntity().getUsername(), "修改岗位失败",
        			"账号已失效，请重新登录", "");
			return R.error("账号已失效，请重新登录");
		}
		Date nowData=new Date();
		dictJob.setUpdateby(user.getUsername());
		dictJob.setUpdatetime(nowData);
		dictJobService.update(dictJob);
		Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
    			ShiroUtils.getUserEntity().getUsername(), "修改岗位成功",
    			"", "");
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("dictjob:delete")
	public R delete(@RequestBody String[] ids){
		dictJobService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
